package com.cloudwise.gproject.controller;

import com.cloudwise.gproject.entity.Progress;
import com.cloudwise.gproject.entity.Upload;
import com.cloudwise.gproject.service.DictionaryService;
import com.cloudwise.gproject.service.ProgressService;
import com.cloudwise.gproject.service.UploadService;
import com.cloudwise.gproject.util.Result;
import com.cloudwise.gproject.util.ReturnStatus;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;


/**
 * (Progress)表控制层
 *
 * @author makejava
 * @since 2021-03-24 09:19:30
 */
@Controller
@RequestMapping("progress")
public class ProgressController {
    /**
     * 服务对象
     */
    @Resource
    private ProgressService progressService;
    @Resource
    private UploadService uploadService;
    @Resource
    private DictionaryService dictionaryService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @ResponseBody
    @RequestMapping("selectOne")
    public Result selectOne(Integer id) {
        return new Result(ReturnStatus.SUCCESS.getCode(),
                ReturnStatus.SUCCESS.getMessage(), 0,
                this.progressService.queryById(id));
    }

    /**
     * 跳转部门分页列表页面
     *
     * @return
     */
    @RequestMapping("toProgress")
    public String toProgress() {
        return "back/progress";
    }

    /**
     * 通过实体作为筛选条件查询
     *
     * @return 对象列表
     */
    @ResponseBody
    @RequestMapping("queryAll")
    public Result queryAll(Integer page, Integer limit, Progress progress,HttpServletRequest request) {
        Integer businessId = (Integer)request.getSession().getAttribute("pbusinessId");
        if (businessId!=null){
            progress.setBusinessId(businessId);
        }
        request.getSession().removeAttribute("pbusinessId");
        PageInfo pageInfo = progressService.queryAll(page, limit, progress);
        List list = pageInfo.getList();
        System.out.println(list);
        return new Result(ReturnStatus.SUCCESS.getCode(),
                ReturnStatus.SUCCESS.getMessage(),
                pageInfo.getTotal(), pageInfo.getList());
    }

    /**
     * 新增数据
     *
     * @param progress 实例对象
     * @return 实例对象
     */
    @ResponseBody
    @RequestMapping("insert")
    public Result insert(Progress progress,String uploadSourceName,String uploadDtName) {
        if (uploadDtName!=null&&uploadDtName!=""&&uploadSourceName!=null&&uploadSourceName!=""){
            Upload upload = new Upload();
            upload.setSourceName(uploadSourceName);
            upload.setDestinationName(uploadDtName);
            Upload insert1 = uploadService.insert(upload);
            progress.setUploadId(insert1.getUploadId());
        }
        Progress insert = this.progressService.insert(progress);
        return new Result(ReturnStatus.SUCCESS.getCode(),
                ReturnStatus.SUCCESS.getMessage(), 0,
                insert);
    }

    /**
     * 修改数据
     *
     * @param progress 实例对象
     * @return 实例对象
     */
    @ResponseBody
    @RequestMapping("update")
    public Result update(Progress progress,String uploadSourceName,String uploadDtName) {
        Progress update = progressService.update(progress);
        Upload upload = new Upload(progress.getUploadId(), uploadSourceName, uploadDtName);
        uploadService.update(upload);
        return new Result(ReturnStatus.SUCCESS.getCode(),
                ReturnStatus.SUCCESS.getMessage(), 0,
                update);
    }

    /**
     * 通过主键删除数据
     *
     * @param progressId 主键
     * @return 是否成功
     */
    @ResponseBody
    @RequestMapping("deleteById")
    public Result deleteById(Integer progressId) {
        return new Result(ReturnStatus.SUCCESS.getCode(),
                ReturnStatus.SUCCESS.getMessage(), 0,
                this.progressService.deleteById(progressId));
    }

    /** 
     * @create by: 卫子健
     * @description: 显示客户信息和业务信息 
     * @create time: 2021/3/24 22:05
     * @param  
     * @return  
     */
    @GetMapping("showBusinessAndProgress")
    public ModelAndView showBusinessAndProgress(Integer customId, Integer businessId, ModelAndView mv, HttpServletRequest request) {
        //为business.html的“修改进度”超链接，保存数据，并跳转页面
        request.getSession().setAttribute("kbusinessId",businessId);
        request.getSession().setAttribute("kcustomId",customId);
        request.getSession().setAttribute("pbusinessId",businessId);
        mv.addObject("customId", customId);
        mv.addObject("businessId", businessId);
        mv.setViewName("back/progress");
        return mv;
    }

    /** 
     * @create by: 卫子健
     * @description: 按照时间为横坐标展示进度 
     * @create time: 2021/3/25 16:53
     * @param loginName
     * @param processId 
     * @return com.cloudwise.trademark.util.Result 
     */
    @ResponseBody
    @RequestMapping("progressEchartTime")
    public Result progressEchartTime(String loginName,Integer processId){
        if (processId != null && !"".equals(processId)) {
            Progress progress = progressService.queryById(processId);
            Map<String, Object> map = progressService.progressEchartTime(loginName, progress.getProgressType());
            return new Result(ReturnStatus.SUCCESS.getCode(),
                    ReturnStatus.SUCCESS.getMessage(), 0,
                    map);
        } else {
            Map<String, Object> map = progressService.progressEchartTime(loginName, null);
            return new Result(ReturnStatus.SUCCESS.getCode(),
                    ReturnStatus.SUCCESS.getMessage(), 0,
                    map);
        }
    }

    @ResponseBody
    @RequestMapping("progressEchartName")
    public Result progressEchartName(String startTime,String endTime,Integer progressId){
        if (startTime == null || "".equals(startTime)) {
            startTime = "2000-01-01";
        }
        if (endTime == null || "".equals(endTime)) {
            endTime = "2100-01-01";
        }
        if (progressId != null && !"".equals(progressId)) {
            Progress progress = progressService.queryById(progressId);
            Map<String, Object> map = progressService.progressEchartName(startTime, endTime, progress.getProgressType());
            return new Result(ReturnStatus.SUCCESS.getCode(),
                    ReturnStatus.SUCCESS.getMessage(), 0,
                    map);
        } else {
            Map<String, Object> map = progressService.progressEchartName(startTime, endTime, null);
            return new Result(ReturnStatus.SUCCESS.getCode(),
                    ReturnStatus.SUCCESS.getMessage(), 0,
                    map);
        }
    }
}
